package IO;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileExample {

//    public static void main(String[] args) throws IOException {
//        createDirectory("D:\\文件夹1");
//        createDirectory("D:\\文件夹2");
//    }
    public static void createDirectory(String directory) throws IOException {
        File file = new File(directory);
        // 如果文件夹不存在
        if(!file.exists())
            // 创建文件夹
            file.mkdir();
    }

//    public static void main(String[] args) throws IOException {
////        createDirectory();
//
//        File zipFile = createZipFile("D:/test.zip");
//        // 新建TXT文件，并写入文本
//        createAndWriteTXTFileInZipFile(zipFile);
//
//        // 新建test文件夹
////        createDirectoryInZipFile(zipFile,"test1\\");
////        createDirectoryInZipFile(zipFile,"test2\\");
//
//        // 这样添加两个，但是却只有一个，456.txt，123.txt 不见了
//
////        createDirectoryInZipFile(zipFile,"test\\123.txt");
//
////        createDirectoryInZipFile(zipFile,"test\\456.txt");
//
////        FileOutputStream fOutputStream = new FileOutputStream(zipFile);
////        ZipOutputStream zoutput = new ZipOutputStream(fOutputStream);
////        zipWrite("test\\123.txt",zoutput);
//
////        zipWrite("test\\456.txt",zoutput);
//
//    }

    private static File createDirectory() {
        File file = new File("D:\\test");
        // 如果文件夹不存在
        if(!file.exists())
            // 创建文件夹
            file.mkdir();
        return file;
    }

    /**
     * 新建一个压缩包
     * @param realPath
     * @throws IOException
     */
    private static File createZipFile(String realPath) throws IOException {
        File file = new File(realPath);
        // 如果文件不存在
        if(!file.exists())
            // 创建文件
            file.createNewFile();

        return file;
    }

    /**
     * 在压缩包里面新建文件夹
     * @param zipFile
     */
    public static void createDirectoryInZipFile(File zipFile, String path) throws IOException {
        // 输出流，写入文件内容
        FileOutputStream fOutputStream = new FileOutputStream(zipFile);
        ZipOutputStream zoutput = new ZipOutputStream(fOutputStream);

        zoutput.putNextEntry(new ZipEntry(path));
        zoutput.closeEntry();
        zoutput.close();
    }

    /**
     * 新建TXT文件，并写入文本
     * @param args
     */
    public static void main(String[] args) throws IOException {
        File zipFile = createZipFile("D:/test.zip");
        String txt = "test";
        createAndWriteTXTFileInZipFile(zipFile,txt);
    }

    /**
     *
     * @param file
     * @throws IOException
     */
    private static void createAndWriteTXTFileInZipFile(File file,String txt) throws IOException {
        // 新建文本
//        String string="test ZIP!";
        String string=txt;
        byte[] buffer =string.getBytes();

        // 输出流，写入文件内容
        FileOutputStream fOutputStream = new FileOutputStream(file);
        ZipOutputStream zoutput = new ZipOutputStream(fOutputStream);

        //
        ZipEntry zEntry  = new ZipEntry("test.txt");
        zoutput.putNextEntry(zEntry);
        zoutput.write(buffer);
        zoutput.closeEntry();
        zoutput.close();
    }


    public static void zipWrite(String fileName, ZipOutputStream zOut) throws IOException {
        final FileInputStream fis = new FileInputStream(fileName);
        BufferedInputStream bis = new BufferedInputStream(fis);
        int count;
        byte data[] = new byte[1024];
        while ((count = bis.read(data,0,1024))!=-1){
            zOut.write(data,0,count);
        }
        bis.close();
        fis.close();
    }

}
